﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Threading;
using WSManager.Model;

namespace WSManager.Wpf
{
    public class WebViewModel : ViewModelBase<Web>
    {

        public WebViewModel(Web web)
        {
            if (web == null) throw new ArgumentNullException("web");
            this.Item = web.Repair();
        }

        #region properties

        public string Name
        {
            get
            {
                return Item.Name;
            }
            set
            {
                Item.Name = value;
                RaisePropertyChanged("Name");
            }
        }

        List<ArticleViewModel> _articles;
        public IList<ArticleViewModel> Articles
        {
            get
            {
                if (_articles == null && Item != null)
                {
                    _articles = new List<ArticleViewModel>(Item.Articles.Select(i => new ArticleViewModel(i, this.Item)));
                }
                return _articles;
                //coll.CollectionChanged += articles_CollectionChanged;
                //return coll;
            }
        }
        List<CategoryViewModel> _categories;
        public IList<CategoryViewModel> Categories
        {
            get
            {
                if (_categories == null && Item != null)
                {
                    _categories = Item.Categories.Select(c => new CategoryViewModel(c)).ToList();
                }
                return _categories;
            }
        }

        #endregion

        #region commands

        public ICommand NewArticle { get { return new RelayCommand(CreateArticle); } }



        public ICommand RemoveArticleCommand { get { return new RelayCommand<Article>(RemoveArticle); } }

        public ICommand RemoveArticlesCommand
        {
            get
            {
                return new RelayCommand<IEnumerable<ArticleViewModel>>(list => RemoveArticles(from i in list where i.Checked select i.Article));
            }
        }

        public ICommand NewCategoryCommand
        {
            get
            {
                return new RelayCommand<CategoryViewModel>(c => CreateCategory(c));
            }
        }

        public ICommand RemoveCategoriesCommand { get { return new RelayCommand<CategoryViewModel>(RemoveSelectedCategories); } }

        #endregion
        private void CreateArticle()
        {
            ArticleEditor.ArticleEditorManager.BeginCreateArticle(AddArticle, this.Item);
        }
        public void AddArticle(Article article)
        {
            this.Item.Articles = Item.Articles.Concat(new[] { article });
            _articles = null;
            RaisePropertyChanged("Articles");
        }
        public void RemoveArticle(Article item)
        {
            this.Item.Articles = from a in Item.Articles where a != item select a;
            _articles = null;
            RaisePropertyChanged("Articles");
        }
        public void RemoveArticles(IEnumerable<Article> articles)
        {
            Item.Articles = Item.Articles.Except(articles);
            _articles = null;
            RaisePropertyChanged("Articles");
        }
        public void CreateCategory(Category parentCategory = null)
        {
            Dialogs.NewCategoryDialog.Show(c => AddCategory(c, parentCategory));
        }
        public void AddCategory(Category item, Category parentCategory = null)
        {
            if (parentCategory == null)
            {
                Item.Categories = Item.Categories.Concat(new[] { item });
                _categories = null;
            }
            else
            {

                parentCategory.Subcategories = parentCategory.Subcategories.Concat(new[] { item }).ToList();
                Item.Categories = _categories.Select(c => c.Category);
                _categories = null;
            }
            RaisePropertyChanged("Categories");
        }
        public void RemoveSelectedCategories(CategoryViewModel selectedCategory)
        {
            var categories = Categories.ToArray();
            if (!categories.Recursive().Any(c => c.Checked == true))
                selectedCategory.Checked = true;

            foreach (var item in categories)
            {
                if (item.Checked == true)
                {
                    Item.Categories = Item.Categories.Except(new[] { item.Category });
                }
                else
                {
                    item.RemoveSelectedSubcategories();
                }
            }
            _categories = null;
            RaisePropertyChanged("Categories");
        }

        public Web Web
        {
            get
            {
                return Item;
            }
            set
            {
                Item = value;
            }
        }
    }
}
